package com.tebiecloud.base.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.base.client.model.entity.BaseMenu;
import com.tebiecloud.base.server.service.BaseMenuService;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Map;

/**
 * 系统资源-菜单信息 前端控制器
 * @author tebie
 * @date 2019-08-02
 */
@RestController
@RequestMapping("basemenu")
public class BaseMenuController {

    @Autowired
    private BaseMenuService baseMenuService;

    /**
     * 获取分页数据
     * @param map
     * @return
     */
    @GetMapping(value = "/list")
    public ResultBody<IPage<BaseMenu>> list(@RequestParam(required = false) Map map) {
        PageParams pageParams = new PageParams(map);
        BaseMenu query = pageParams.mapToObject(BaseMenu.class);
        QueryWrapper<BaseMenu> queryWrapper = new QueryWrapper();
        return ResultBodyUtils.ok(baseMenuService.page(new PageParams(map), queryWrapper));
    }

    /**
     * 根据ID查找数据
     * @param id
     * @return
     */
    @GetMapping("/get")
    public ResultBody<BaseMenu> get(@RequestParam("id") Long id) {
        BaseMenu entity = baseMenuService.getById(id);
        return ResultBodyUtils.ok(entity);
    }

    /**
     * 添加数据
     * @param parentId 父级菜单,
     * @param menuCode 菜单编码,
     * @param menuName 菜单名称,
     * @param menuDesc 描述,
     * @param scheme 路径前缀,
     * @param path 请求路径,
     * @param icon 菜单标题,
     * @param target 打开方式:_self窗口内,_blank新窗口,
     * @param priority 优先级 越小越靠前,
     * @param status 状态:0-无效 1-有效,
     * @param isPersist 保留数据0-否 1-是 不允许删除,
     * @param serviceId 服务名
     * @return
     */
    @PostMapping("/add")
    public ResultBody add(
        @RequestParam(value = "parentId", required = false) Long parentId,
        @RequestParam(value = "menuCode", required = false) String menuCode,
        @RequestParam(value = "menuName", required = false) String menuName,
        @RequestParam(value = "menuDesc", required = false) String menuDesc,
        @RequestParam(value = "scheme", required = false) String scheme,
        @RequestParam(value = "path", required = false) String path,
        @RequestParam(value = "icon", required = false) String icon,
        @RequestParam(value = "target", required = false) String target,
        @RequestParam(value = "priority", required = false) Long priority,
        @RequestParam(value = "status", required = false) Integer status,
        @RequestParam(value = "isPersist", required = false) Integer isPersist,
        @RequestParam(value = "serviceId", required = false) String serviceId
    ) {
        BaseMenu entity = new BaseMenu();
        entity.setParentId(parentId);
        entity.setMenuCode(menuCode);
        entity.setMenuName(menuName);
        entity.setMenuDesc(menuDesc);
        entity.setScheme(scheme);
        entity.setPath(path);
        entity.setIcon(icon);
        entity.setTarget(target);
        entity.setPriority(priority);
        entity.setStatus(status);
        entity.setIsPersist(isPersist);
        entity.setServiceId(serviceId);
        baseMenuService.save(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 更新数据
     * @param parentId 父级菜单
     * @param menuCode 菜单编码
     * @param menuName 菜单名称
     * @param menuDesc 描述
     * @param scheme 路径前缀
     * @param path 请求路径
     * @param icon 菜单标题
     * @param target 打开方式:_self窗口内,_blank新窗口
     * @param priority 优先级 越小越靠前
     * @param status 状态:0-无效 1-有效
     * @param isPersist 保留数据0-否 1-是 不允许删除
     * @param serviceId 服务名
     * @return
     */
    @PostMapping("/update")
    public ResultBody update(
            @RequestParam(value = "menuId", required = false) Long menuId,
            @RequestParam(value = "parentId", required = false) Long parentId,
            @RequestParam(value = "menuCode", required = false) String menuCode,
            @RequestParam(value = "menuName", required = false) String menuName,
            @RequestParam(value = "menuDesc", required = false) String menuDesc,
            @RequestParam(value = "scheme", required = false) String scheme,
            @RequestParam(value = "path", required = false) String path,
            @RequestParam(value = "icon", required = false) String icon,
            @RequestParam(value = "target", required = false) String target,
            @RequestParam(value = "priority", required = false) Long priority,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "isPersist", required = false) Integer isPersist,
            @RequestParam(value = "serviceId", required = false) String serviceId
    ) {
        BaseMenu entity = new BaseMenu();
        entity.setMenuId(menuId);
        entity.setParentId(parentId);
        entity.setMenuCode(menuCode);
        entity.setMenuName(menuName);
        entity.setMenuDesc(menuDesc);
        entity.setScheme(scheme);
        entity.setPath(path);
        entity.setIcon(icon);
        entity.setTarget(target);
        entity.setPriority(priority);
        entity.setStatus(status);
        entity.setIsPersist(isPersist);
        entity.setServiceId(serviceId);
        baseMenuService.updateById(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 删除数据
     * @param id 单个id
     * @param ids 多个id用,号隔开
     * @return
     */
    @PostMapping("/remove")
    public ResultBody remove(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "ids", required = false) String ids
    ) {
        if(id != null && id > 0) {
            baseMenuService.removeById(id);
        }
        if(ids != null && ids.length() > 0) {
            baseMenuService.removeByIds(Arrays.asList(ids.split(",")));
        }
        return ResultBodyUtils.ok();
    }

}
